/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.agilelang.editor.view.filesystem;

/**
 *
 * @author RoderickNestor
 */
import java.awt.Dimension;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;
import java.io.*;
import javax.swing.tree.*;

public class MyPanel extends JPanel implements Runnable {

    private JTree tree = new JTree();

    public JTree getTree() {
        return tree;
    }

    public void setTree(JTree tree) {
        this.tree = tree;
    }
    private SimpleFileManager simpleFileManager;

    public MyPanel(String path) throws IOException {
         tree.setPreferredSize( new Dimension( 200, 800 ) );
        this.setMinimumSize( new Dimension( 200, 800 ) );
         JScrollPane jp = new JScrollPane(tree);
         jp.setMinimumSize(new Dimension( 200, 800 ));
        add(jp);
        //File root = new File(System.getProperty("d:"));
        File root = new File(path);
        simpleFileManager = new SimpleFileManager(root, new ImageFileFilter());
        tree.setModel(new DefaultTreeModel(simpleFileManager.getDirectoryTree()));

        /*MouseListener ml = new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                int selRow = tree.getRowForLocation(e.getX(), e.getY());
                TreePath selPath = tree.getPathForLocation(e.getX(), e.getY());
                if (selRow != -1) {
                    if (e.getClickCount() == 1) {
                    } else if (e.getClickCount() == 2) {
                        DefaultMutableTreeNode parent = (DefaultMutableTreeNode) selPath.getLastPathComponent();
                        Object[] os = selPath.getParentPath().getPath();
                        for (Object o : os) {
                            System.out.println("p :" + o);
                        }

                        System.out.println("Doble click :" + parent);

                    }
                }
            }
        };
        tree.addMouseListener(ml);*/
        tree.setCellRenderer(new RendererTree("C:\\Users\\RoderickNestor\\Documents\\test4"));
    }

  // This has to be a separate method, because it is dangerous to leak
    // a "this" pointer from a constructor!
    public void activateMonitor() {
        simpleFileManager.startMonitoring();
        FileMonitor fileMonitor = simpleFileManager.getFileMonitor();
        fileMonitor.addClient(this);

    }

  // The FileManager will call the run() method from the eventqueue, when
    // the tree needs to be rebuild.
    public void run() {
        simpleFileManager.refresh();
        tree.setModel(new DefaultTreeModel(simpleFileManager.getDirectoryTree()));
    }

    // Just for testing
    public static void main(String[] args) throws IOException {
        JFrame frame = new JFrame();
        MyPanel myPanel = new MyPanel("C:\\Users\\RoderickNestor\\Documents");
        myPanel.activateMonitor();
        frame.setContentPane(myPanel);
        frame.pack();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }

    public void  listener(MouseListener ml){
        tree.addMouseListener(ml);
    }
}
